/*
 * 功能：洲、国家、州、城市四级联动
 */
(function(factory){
    if(typeof define === 'function' && define.amd){
        define(['jquery','../../../Common/catedb','select'],factory);
    }else{
        factory(window.jQuery);
    }
}(function($){
    /*洲、国家、州、城市四级联动=====================================*/
    $.fn.country = function(o) {
        var that = $.extend({},$.fn.country.defaults, o);
        return this.each(function() {
            var t = $(this);
            var arthtml='<select name="'+that.a+'" id="'+that.a+'" class="'+that.a+'_item mr10"><option value="">洲</option></select>';
            if(that.level > 1){
                arthtml += '<select name="'+that.b+'" id="'+that.b+'" class="'+that.b+'_item mr10"><option value="">国家</option></select>';
            }
            if(that.level > 2){
                arthtml += '<select name="'+that.c+'" id="'+that.c+'" class="'+that.c+'_item mr10"><option value="">州/省</option></select>';
            }
            if(that.level > 3){
                arthtml += '<select name="'+that.d+'" id="'+that.d+'" class="'+that.d+'_item"><option value="">城市</option></select>';
            }

            t.html(arthtml);
            //var wrap=$(t),
            var continent=t.find("."+that.a+"_item");
            if(that.level > 1){
                var country=t.find("."+that.b+"_item");
            }
            if(that.level > 2){
                var state=t.find("."+that.c+"_item");
            }
            if(that.level > 3){
                var city=t.find("."+that.d+"_item");
            }

            var ThatData=LocalData['country'][0];
            //console.log(ThatData);
            var tdataB=[],tdataC=[],tdataD=[];
            for(var i in ThatData){
                continent.append('<option value="'+ThatData[i].path+'" data-path="'+ThatData[i].path+'">'+ThatData[i].name+'</option>');
            }

            function gethtml(s,id){
                switch(s){
                    //构造国家
                    case 1:
                        tdataB=ThatData[id].child;
                        for(var i in tdataB){
                            country.append('<option value="'+tdataB[i].id+'" data-path="'+tdataB[i].path+'">'+tdataB[i].name+'</option>');
                        }
                        break;

                    //构造州
                    case 2:
                        tdataC=tdataB[id].child;
                        for(var i in tdataC){
                            state.append('<option value="'+tdataC[i].id+'" data-path="'+tdataC[i].path+'">'+tdataC[i].name+'</option>');
                        }
                        break;

                    //构造城市
                    case 3:
                        tdataD=tdataC[id].child;
                        for(var i in tdataD){
                            city.append('<option value="'+tdataD[i].id+'" data-path="'+tdataD[i].path+'">'+tdataD[i].name+'</option>');
                        }
                        break;
                }
            };

            function selects(){
                $(that.input).val('');
                if(that.level>3){
                    if(city.val() && typeof(tdataD[city.val()].child)=='undefined'){
                        var c=city.find('option:selected');
                        $(that.input).val(c.data('path'));
                        return false;
                    }
                }
                if(that.level>2){
                    if(state.val() && (that.minSelectLevel<= 3 || typeof(tdataC[state.val()].child)=='undefined')){
                        var s=state.find('option:selected');
                        $(that.input).val(s.data('path'));
                        return false;
                    }
                }
                if(that.level>1){
                    if(country.val() && (that.minSelectLevel<= 2 || typeof(tdataB[country.val()].child)=='undefined')){
                        var c=country.find('option:selected');
                        $(that.input).val(c.data('path'));
                        return false;
                    }
                }

                if(continent.val() && that.minSelectLevel <= 1){
                    var con=continent.find('option:selected');
                    $(that.input).val(con.data('path'));
                    return false;
                }
            };

            initdb();
            /*初始化数据===============================*/
            function initdb(){
                var input=$(that.input).val();
                var idsarr=[];
                if(input != ''){
                    idsarr = input.split("-");

                    if(idsarr.length > 0){
                        continent.find('[value="'+idsarr[0]+'"]').attr('selected',true);
                        gethtml(1,idsarr[0]);
                    }

                    if(idsarr.length > 1){
                        country.find('[value="'+idsarr[1]+'"]').attr('selected',true);
                        gethtml(2,idsarr[1]);
                    }

                    if(idsarr.length > 2){
                        if(state.find('[value="'+idsarr[2]+'"]').length){
                            state.find('[value="'+idsarr[2]+'"]').attr('selected',true);
                            gethtml(3,idsarr[2]);
                        } else if(city.find('[value="'+idsarr[2]+'"]').length){
                            city.find('[value="'+idsarr[2]+'"]').attr('selected',true);
                        }
                    }

                    if(idsarr.length > 3){
                        city.find('[value="'+idsarr[3]+'"]').attr('selected',true);
                    }
                    selects();
                }
            };
            var _continent,_country,_state,_city;
            $(continent).selectbox({w:(that.width?that.width[0]:''),cutstr:(that.cutstr?that.cutstr[0]:''),divclass:that.sclass,callback:function(t){
                switch(that.level){
                    case 2:
                        country.children(':not(:first)').remove();
                        _country.init();
                        var path=t.attr('v');
                        if(path==='') return false;
                        gethtml(1,path);
                        selects();
                        _country.init();
                        break;

                    case 3:
                        country.children(':not(:first)').remove();
                        state.children(':not(:first)').remove();
                        _country.init();
                        _state.init();
                        var path = t.attr('v');
                        if(path==='') return false;
                        gethtml(1,path);
                        selects();
                        _country.init();
                        _state.init();
                        break;

                    case 4:
                        country.children(':not(:first)').remove();
                        state.children(':not(:first)').remove();
                        city.children(':not(:first)').remove();
                        _country.init();
                        _state.init();
                        _city.init();
                        var path=t.attr('v');
                        if(path==='') return false;
                        gethtml(1,path);
                        selects();
                        _country.init();
                        _state.init();
                        _city.init();
                        break;
                }
            }},function(){
                _continent=this;
            });
            $(country).selectbox({w:(that.width?that.width[1]:''),cutstr:(that.cutstr?that.cutstr[1]:''),divclass:that.sclass,callback:function(t){
                switch(that.level){
                    case 3:
                        state.children(':not(:first)').remove();
                        _state.init();
                        var path=t.attr('v');
                        if(path==='')return false;
                        gethtml(2,path);
                        selects();
                        _state.init();
                        break;

                    case 4:
                        state.children(':not(:first)').remove();
                        city.children(':not(:first)').remove();
                        _state.init();
                        _city.init();
                        var path=t.attr('v');
                        if(path==='')return false;
                        gethtml(2,path);
                        selects();
                        _state.init();
                        _city.init();
                        break;
                }
            }},function(){
                _country=this;
            });
            $(state).selectbox({w:(that.width?that.width[1]:''),cutstr:(that.cutstr?that.cutstr[1]:''),divclass:that.sclass,callback:function(t){
                if(that.level == 4){
                    city.children(':not(:first)').remove();
                    _city.init();
                    var path=t.attr('v');
                    if(path==='')return false;
                    gethtml(3,path);
                    selects();
                    _city.init();
                }
            }},function(){
                _state=this;
            });
            $(city).selectbox({w:(that.width?that.width[2]:''),cutstr:(that.cutstr?that.cutstr[2]:''),divclass:that.sclass,callback:function(t){
                selects();
            }},function(){
                _city=this;
            });
            
        });
    };
    $.fn.country.defaults = {a:'continent', b:'country', c:'state',d:'city',level:4,minSelectLevel:4,sclass:''};
}));